﻿// =================================================================== 
// 数据访问层(GDIT.DAL)
//====================================================================
// 文件：DCollegeInformationArticle.cs
// 项目名称：学院网站
// 创建时间：2012-08-08
// 负责人：ChenZhiGang
//引用DevExpress.Data.Filtering，DevExpress.Xpo程序集
// 程序集介绍：校园资讯文章信息数据访问层
// ===================================================================
using GDIT.Model;
using DevExpress.Data.Filtering;
using DevExpress.Xpo;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GDIT.Pattern;
using System.Data;
using DevExpress.Xpo.DB;

namespace GDIT.DAL
{
    public class DCollegeInformationArticle : ICollegeInformationArticle
    {

        private XpoHelper.Test<Mtb_CollegeInformationArticle> userOperation = null;

        /// <summary>
        /// 管理员展示页面
        /// </summary>
        /// <returns></returns>
        public XPCollection SelectAllColegeByAdmin()
        {
            userOperation = new XpoHelper.Test<Mtb_CollegeInformationArticle>();
            return userOperation.Operation();
        }

        /// <summary>
        /// 新增加一篇校园资讯
        /// </summary>
        /// <param name="bulletin">需要增加的校园资讯对象</param>
        /// <returns>1：新增成功  0：新增失败</returns>
        public int InsertCollegeInformationArticle(Mtb_CollegeInformationArticle bulletin)
        {
            return XpoHelper.SaveAdded(bulletin);
        }

        /// <summary>
        /// 根据关键字查询并返回校园资讯集合
        /// </summary>
        /// <param name="keyWord">输入的关键字</param>
        /// <returns></returns>
        public XPCollection SelectCollegeByKeyWord(string keyWord)
        {
            XPCollection xp = new XPCollection(typeof(Mtb_CollegeInformationArticle), new ContainsOperator("KeyWords", new BinaryOperator("AritlceKeyWord", "%" + keyWord + "%", BinaryOperatorType.Like)));
            return xp;
        }

        /// <summary>
        /// 删除一篇校园资讯
        /// </summary>
        /// <param name="bulletin">需要删除的校园资讯对象</param>
        /// <returns>1：删除成功  0：删除失败</returns>
        public int DeleteCollegeInformationArticle(int id)
        {
            userOperation = new XpoHelper.Test<Mtb_CollegeInformationArticle>();
            return userOperation.Delete(id);
        }

        /// <summary>
        /// 更新一篇校园资讯标题和内容
        /// </summary>
        /// <param name="bulletin">需要更新的校园资讯的对象</param>
        /// <returns>1：删除成功  0：删除失败</returns>
        public int UpdateCollegeInformationArticle(Mtb_CollegeInformationArticle bulletin)
        {
            userOperation = new XpoHelper.Test<Mtb_CollegeInformationArticle>();
            XPCollection xp = userOperation.Operation("OID", bulletin.OID);
            foreach (Mtb_CollegeInformationArticle b in xp)
            {
                b.Title = bulletin.Title;
                b.Contents = bulletin.Contents;
                b.Top = bulletin.Top;
                b.Important = bulletin.Important;
                b.Internal = bulletin.Internal;
                b.KeyWords.AddRange(bulletin.KeyWords);
                return XpoHelper.SaveModified(b);
            }
            return 0;
        }

        /// <summary>
        /// 增加校园资讯的点击率
        /// </summary>
        public int AddCollegeInformationArticleCounter(int id)
        {
            userOperation = new XpoHelper.Test<Mtb_CollegeInformationArticle>();
            XPCollection xp = userOperation.Operation("OID", id);
            foreach (Mtb_CollegeInformationArticle b in xp)
            {
                b.Counter = b.Counter + 1;
                return XpoHelper.SaveModified(b);
            }
            return 0;
        }

        /// <summary>
        /// 查询所有非置顶，重点，内部的校园资讯列表
        /// </summary>
        /// <returns>返回所有的校园资讯列表</returns>
        public XPCollection SelectAllCollegeInformationArticle()
        {
            GroupOperator criteria = new GroupOperator();
            criteria.Operands.Add(new BinaryOperator("Top", "否", BinaryOperatorType.Equal));
            criteria.Operands.Add(new BinaryOperator("Internal", "否", BinaryOperatorType.Equal));
            criteria.Operands.Add(new BinaryOperator("Important", "否", BinaryOperatorType.Equal));
            userOperation = new XpoHelper.Test<Mtb_CollegeInformationArticle>();
            return userOperation.Operation(criteria);
        }

        /// <summary>
        /// 查询所有非置顶，重点，是内部的校园资讯列表
        /// </summary>
        /// <returns>返回所有的校园资讯列表</returns>
        public XPCollection SelectAllInternalCollegeInformationArticle()
        {
            GroupOperator criteria = new GroupOperator();
            criteria.Operands.Add(new BinaryOperator("Top", "否", BinaryOperatorType.Equal));
            criteria.Operands.Add(new BinaryOperator("Important", "否", BinaryOperatorType.Equal));
            criteria.Operands.Add(new BinaryOperator("Internal", "是", BinaryOperatorType.Equal));
            userOperation = new XpoHelper.Test<Mtb_CollegeInformationArticle>();
            return userOperation.Operation(criteria);
        }

        /// <summary>
        /// 根据校园资讯Id查询并返回校园资讯单一记录
        /// </summary>
        /// <param name="id">需要查询的校园资讯Id</param>
        /// <returns>返回校园资讯单一记录</returns>
        public Mtb_CollegeInformationArticle SelectCollegeInformationArticleById(int id)
        {
            Mtb_CollegeInformationArticle bulletin = null;
            userOperation = new XpoHelper.Test<Mtb_CollegeInformationArticle>();
            XPCollection xp = userOperation.Operation("OID", id);
            foreach (Mtb_CollegeInformationArticle b in xp)
            {
                bulletin = b;
            }
            return bulletin;
        }

        /// <summary>
        /// 根据部门名称查询并返回该部门的所有校园资讯列表
        /// </summary>
        /// <param name="name">需要查询的部门名称</param>
        /// <returns>返回该部门的校园资讯列表</returns>
        public XPCollection SelectCollegeInformationArticleByDepartment(string departmentName)
        {
            XPCollection collection = new XPCollection(typeof(Mtb_CollegeInformationArticle),
      new ContainsOperator("Publisher", new ContainsOperator("Department", new BinaryOperator("DepartmentName", departmentName, BinaryOperatorType.Equal))));
            return collection;
        }

        /// <summary>
        /// 显示置顶校园资讯
        /// </summary>
        /// <returns>返回置顶校园资讯列表</returns>
        public XPCollection SelectCollegeInformationArticleByTop()
        {
            GroupOperator criteria = new GroupOperator();
            criteria.Operands.Add(new BinaryOperator("Top", "是", BinaryOperatorType.Equal));
            criteria.Operands.Add(new BinaryOperator("Internal", "否", BinaryOperatorType.Equal));
            userOperation = new XpoHelper.Test<Mtb_CollegeInformationArticle>();
            XPCollection xp = userOperation.Operation(criteria);
            //按照发布时间进行降序排列
            SortProperty sp = new SortProperty("PublishDate", DevExpress.Xpo.DB.SortingDirection.Descending);
            xp.Sorting.Add(sp);
            return xp;
        }

        /// <summary>
        /// 显示重点校园资讯
        /// </summary>
        /// <returns>返回重点校园资讯列表</returns>
        public XPCollection SelectCollegeInformationArticleByImportant()
        {
            GroupOperator criteria = new GroupOperator();
            criteria.Operands.Add(new BinaryOperator("Important", "是", BinaryOperatorType.Equal));
            criteria.Operands.Add(new BinaryOperator("Internal", "否", BinaryOperatorType.Equal));
            userOperation = new XpoHelper.Test<Mtb_CollegeInformationArticle>();
            XPCollection xp = userOperation.Operation(criteria);
            //按照发布时间进行降序排列
            SortProperty sp = new SortProperty("PublishDate", DevExpress.Xpo.DB.SortingDirection.Descending);
            xp.Sorting.Add(sp);
            return xp;
        }

        /// <summary>
        /// 显示内部重点学院资讯
        /// </summary>
        /// <returns>返回重点公告列表</returns>
        public XPCollection SelectInternalCollegeInformationArticleByImportant()
        {
            GroupOperator criteria = new GroupOperator();
            criteria.Operands.Add(new BinaryOperator("Top", "是", BinaryOperatorType.Equal));
            criteria.Operands.Add(new BinaryOperator("Internal", "是", BinaryOperatorType.Equal));
            userOperation = new XpoHelper.Test<Mtb_CollegeInformationArticle>();
            XPCollection xp = userOperation.Operation(criteria);
            //按照发布时间进行降序排列
            SortProperty sp = new SortProperty("PublishDate", DevExpress.Xpo.DB.SortingDirection.Descending);
            xp.Sorting.Add(sp);
            return xp;
        }

        /// <summary>
        /// 显示内部置顶校园资讯
        /// </summary>
        /// <returns>返回置顶公告列表</returns>
        public XPCollection SelectInternalCollegeInformationArticleByTop()
        {
            GroupOperator criteria = new GroupOperator();
            criteria.Operands.Add(new BinaryOperator("Important", "是", BinaryOperatorType.Equal));
            criteria.Operands.Add(new BinaryOperator("Internal", "是", BinaryOperatorType.Equal));
            userOperation = new XpoHelper.Test<Mtb_CollegeInformationArticle>();
            XPCollection xp = userOperation.Operation(criteria);
            //按照发布时间进行降序排列
            SortProperty sp = new SortProperty("PublishDate", DevExpress.Xpo.DB.SortingDirection.Descending);
            xp.Sorting.Add(sp);
            return xp;
        }

        /// <summary>
        /// 设置校园资讯为内部显示
        /// </summary>
        /// <returns>返回内部校园资讯列表</returns>
        public int SelectCollegeInformationArticleByInternal(Mtb_CollegeInformationArticle college)
        {
            userOperation = new XpoHelper.Test<Mtb_CollegeInformationArticle>();
            XPCollection xp = userOperation.Operation("OID", college.OID);
            foreach (Mtb_BulletinArticle b in xp)
            {
                b.Internal = college.Internal;
                return XpoHelper.SaveModified(b);
            }
            return 0;
        }

        /// <summary>
        /// 设置校园资讯为重点显示
        /// </summary>
        /// <returns>1：更新成功  0：更新失败</returns>
        public int UpdateCollegeInformationArticleByImportant(Mtb_CollegeInformationArticle college)
        {
            userOperation = new XpoHelper.Test<Mtb_CollegeInformationArticle>();
            XPCollection xp = userOperation.Operation("OID", college.OID);
            foreach (Mtb_BulletinArticle b in xp)
            {
                b.Important = college.Important;
                return XpoHelper.SaveModified(b);
            }
            return 0;
        }

        /// <summary>
        /// 设置校园资讯为置顶显示
        /// </summary>
        /// <returns>1：更新成功  0：更新失败</returns>
        public int UpdateCollegeInformationArticleByTop(Mtb_CollegeInformationArticle college)
        {
            userOperation = new XpoHelper.Test<Mtb_CollegeInformationArticle>();
            XPCollection xp = userOperation.Operation("OID", college.OID);
            foreach (Mtb_BulletinArticle b in xp)
            {
                b.Top = college.Top;
                return XpoHelper.SaveModified(b);
            }
            return 0;
        }

        /// <summary>
        /// 显示首页的校园资讯内容
        /// </summary>
        /// <returns></returns>
        public XPCollection SelectCollegeInformationArticleByTopAndImportant()
        {
            XPCollection xp = new XPCollection(typeof(Mtb_CollegeInformationArticle));
            xp.AddRange(SelectCollegeInformationArticleByTop());
            xp.AddRange(SelectCollegeInformationArticleByImportant());
            xp.AddRange(SelectAllCollegeInformationArticle());
            return xp;
        }

        /// <summary>
        /// 显示内部首页的校园资讯内容
        /// </summary>
        /// <returns></returns>
        public XPCollection SelectInternalBulletinArticleByTopAndImportant()
        {
            XPCollection xp = new XPCollection(typeof(Mtb_CollegeInformationArticle));
            xp.AddRange(SelectInternalCollegeInformationArticleByTop());
            xp.AddRange(SelectInternalCollegeInformationArticleByImportant());
            xp.AddRange(SelectAllInternalCollegeInformationArticle());
            return xp;
        }
    }
}
